<template>
  <div class="login_container">
    <div class="login_box">
      <div class="touxiang_box">
        <img src="../assets/logo.jpg" alt />
      </div>
      <!--  ref可以得到表单的引用对象 -->
      <el-form ref="loginFormRef" :model="Form" :rules="rules" label-width="0px" class="lg_from">
        <el-form-item prop="username">
          <el-input v-model="Form.username" prefix-icon="el-icon-user" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="Form.password" prefix-icon="el-icon-lock" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="denglu">登录</el-button>
          <el-button type="info" @click="resetForm('loginFormRef')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      Form: {
        username: '',
        password: ''
      },
      // 表单验证规则对象
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置
    resetForm() {
      this.$refs.loginFormRef.resetFields()
    },
    denglu() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        // 结构赋值result，data为真实数据 重命名为res
        const { data: res } = await this.$http.post('login', this.Form)
        if (res.status !== 200) return this.$message.error(res.msg)
        this.$message.success(res.msg)
        sessionStorage.setItem('token', res.token)
        sessionStorage.setItem('name', res.name)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
//scoped是vue的指令，代码样式只在当前组件生效，不是全局的，可以防止组件间的样式冲突
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 10px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  //减量位移，横纵各50%
  .touxiang_box {
    height: 100px;
    width: 100px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 8px;
    box-shadow: 0 0 8px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.lg_from {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 30px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: center;
}
</style>
